<?php

namespace App\Http\Service\Order;

use App\Enum\OrderEnum;
use App\Func\User\UserFunc;
use App\Http\Service\BasicInterfaceService;
use App\Models\Order\OrderModel;

class UserOrderService implements BasicInterfaceService
{

    public function listSelectDefaultFilters($query){
        $query->pushFilter('status',1);
    }

    public function getList(array $prams)
    {
        // TODO 这里用的管理员登录做测试
        // $customer = (new CustomerFunc())->getLoginInfo(canAll: true);
        $customer = (new UserFunc())->getLoginInfo();
        $page_size = $prams['page_size'] ?? 10;
        $filters = $prams['filters'] ?? [];
        $desc = $prams['desc'] ?? [];
        $orderModel = new OrderModel();

        // 规定类型为拼团
        $orderModel->pushFilter('user_id',$customer['user_id']);
        $this->listSelectDefaultFilters($orderModel);
        if($filters){
            $this->listSelectFilters($orderModel,$filters);
        }
        $query = $orderModel->with('subs');
        $res = $orderModel->cList($query,$page_size,$desc,[
            'order_xid',
            'order_no',
            'total_amount',
            'pay_status',
            'shipping_name',
            'shipping_phone',
            'shipping_address',
            'order_status',
            'created_at',
            'updated_at'
        ]);
        // 处理数据
        $res['data'] = $this->dispose($res['data']);

        s_c_return($res,trans('message.get_list_ok',[
            'name' => trans('key.order')
        ]));
    }

    public function dispose($data): mixed
    {
        // 修改订单状态
        foreach($data as $key=>$datum){
            $data[$key]['pay_status_str'] = OrderEnum::getStrByEnum($datum['pay_status'],OrderEnum::PAY_STATUS_STR);
            $data[$key]['order_status_str'] = OrderEnum::getStrByEnum($datum['order_status'],OrderEnum::ORDER_STATUS_STR);
        }
        return $data;
    }

    public function listSelectFilters($query, $filters)
    {
        $filters = trim_array($filters);
        if(map_has_value($filters,'company_name')){
            $query->pushFilter('company_name',$filters['company_name'],'like');
        }
        if(map_has_value($filters,'company_code')){
            $query->pushFilter('company_code',$filters['company_code'],'like');
        }
    }

    public function getSelect()
    {

    }

    public function getSelectHasPage($prams)
    {

    }

    public function getInfo($id)
    {

    }

    public function create($data)
    {

    }

    public function fakeDelete($id)
    {

    }

    public function fakeDeleteByIds($ids)
    {
        $orderModel = new OrderModel();
        $orderModel->whereIn('order_id',$ids)->update([
            'status' => 2
        ]);
        s_c_return(null,trans('message.update_ok',[
            'name' => trans('key.order')
        ]));
    }

    public function delete($id)
    {

    }

    public function update($data)
    {

    }
}
